package org.ros.android.android_tutorial_image_transport;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;

import org.ros.exception.RemoteException;
import org.ros.exception.RosRuntimeException;
import org.ros.exception.ServiceNotFoundException;
import org.ros.namespace.GraphName;
import org.ros.node.ConnectedNode;
import org.ros.node.Node;
import org.ros.node.NodeMain;
import org.ros.node.service.ServiceClient;
import org.ros.node.service.ServiceResponseListener;

import java.util.Timer;
import java.util.TimerTask;

import head_arduino_msgs.LightShowRequest;
import head_arduino_msgs.LightShowResponse;

/**
 * Created by corvin on 17-8-23.
 */

public class LightControl implements NodeMain {

    private String serviceName = "/mobilebase_arduino/light_show";
    private static final String TAG = LightControl.class.getSimpleName();
    private ServiceClient<LightShowRequest, LightShowResponse> serviceClient ;
    private Timer publisherTimer;

    public LightControl()
    {

    }

    @Override
    public GraphName getDefaultNodeName() {
        return GraphName.of("myLightControl");
    }

    @Override
    public void onStart(ConnectedNode connectedNode)
    {
        try {
            serviceClient = connectedNode.newServiceClient(serviceName, head_arduino_msgs.LightShow._TYPE);
            Log.w(TAG, "Now in onStart function connect serviceClient...");
        }
        catch (ServiceNotFoundException e) {
            Log.e(TAG, "ServiceNotFoundException e:" + e.toString());
            throw new RosRuntimeException(e);
        }

        publisherTimer = new Timer();
        publisherTimer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                if (MainActivity.sendLightFlag)
                {
                    MainActivity.sendLightFlag = false;
                    Log.w(TAG, "Now in onStart function...");
                    Log.d(TAG, "click light control btn ! value = " + MainActivity.lightIndex);
                    final head_arduino_msgs.LightShowRequest light_request = serviceClient.newMessage();
                    light_request.setValue(MainActivity.lightIndex);
                    serviceClient.call(light_request, new ServiceResponseListener<LightShowResponse>()
                    {
                        @Override
                        public void onSuccess(head_arduino_msgs.LightShowResponse response) {
                        }

                        @Override
                        public void onFailure(RemoteException e) {
                            throw new RosRuntimeException(e);
                        }
                    });
                }
            }
        }, 0, 500);
    }

    @Override
    public void onShutdown(Node node) {

    }

    @Override
    public void onShutdownComplete(Node node) {
        publisherTimer.cancel();
        publisherTimer.purge();
    }

    @Override
    public void onError(Node node, Throwable throwable) {

    }
}
